#include <QtGui>
#include <QLineEdit>
#include <QTextEdit>
#include <QVBoxLayout>
#include <iostream>
using namespace std;

#include "test.h"
#include "qxtglobalshortcut.h"


test::test()
{

  QLineEdit *wordLine = new QLineEdit;
  QLineEdit *phoneticLine = new QLineEdit;
  QTextEdit *transEdit = new QTextEdit;
  
  QVBoxLayout *vl = new QVBoxLayout;
  vl->addWidget(wordLine);
  vl->addWidget(phoneticLine);
  vl->addWidget(transEdit);
  vl->addStretch();
  setLayout(vl);
  
  wordLine->setText("hello");

  QAction *minimizeAction = new QAction(tr("Mi&nimize"), this);
  connect(minimizeAction, SIGNAL(triggered()), this, SLOT(hide()));
  QAction *maximizeAction = new QAction(tr("Ma&ximize"), this);
  connect(maximizeAction, SIGNAL(triggered()), this, SLOT(showMaximized()));
  QAction *restoreAction = new QAction(tr("&Restore"), this);
  connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));
  QAction *quitAction = new QAction(tr("&Quit"), this);
  connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
  QMenu *trayIconMenu = new QMenu(this);
  trayIconMenu->addAction(minimizeAction);
  trayIconMenu->addAction(maximizeAction);
  trayIconMenu->addAction(restoreAction);
  trayIconMenu->addSeparator();
  trayIconMenu->addAction(quitAction);

  trayIcon = new QSystemTrayIcon(this);
  trayIcon->setContextMenu(trayIconMenu);
  trayIcon->setIcon(QIcon(":/images/heart.svg"));
  trayIcon->setToolTip("hello");
  trayIcon->show();

  //global hot key
  QxtGlobalShortcut *sc = new QxtGlobalShortcut(QKeySequence("Alt+Shift+I"));
  connect(sc, SIGNAL(activated()), this, SLOT(showOrHide()));

  setWindowTitle(tr("Steward !"));
  resize(400, 300);
}

void test::closeEvent(QCloseEvent *event)
 {
   /*   if (trayIcon->isVisible()) {
   QMessageBox::information(this, tr("Systray"),
			    tr("The program will keep running in the "
			       "system tray. To terminate the program, "
			       "choose <b>Quit</b> in the context menu "
			       "of the system tray entry."));
   */
   //trayIcon->showMessage("title", "this program will keep running in the system tray", QSystemTrayIcon::Warning, 1*1000);
   hide();
   event->ignore();
 }

void test::showOrHide()
{
  if(isVisible())
    hide();
  else
    showNormal();
}

void test::showOnTop(const QString str)
{
  cout << "received : " << qPrintable(str) << endl;
  hide();
  showNormal();
  //raise();
  //setWindowState(Qt::WindowActive);
  //setFocus();
}
